<template>
  <el-container class="home-container">
    <el-header>
      <div class="logoBox">
        <img src="../assets/logo.png" width="50" alt />
        <span>电商管理平台</span>
      </div>
      <el-button class="btnLogout" type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px' ">
        <div class="toggle-button" @click="toggleMenu">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          :index="item.id + ''"
          v-for="item in menuList"
          :key="item.id"
          :collapse="isCollapse"
          :collapse-transition="isCollapse"
          router
          :default-active="$router.path"
        >
          <el-submenu :index="item.path">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item :index="subItem.path" v-for="subItem in item.childList" :key="subItem.id">
              <i :class="subItem.icon"></i>
              {{ subItem.name }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menu/getList')
      console.log(res)
      if (res.code !== 200) {
        this.$message.error('获取菜单失败')
        return
      }
      this.menuList = res.data
      console.log(JSON.stringify(this.menuList))
    },
    logout() {
      this.$confirm('确定退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          window.sessionStorage.clear()
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
          this.$router.push('/login')
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #ddd;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
  padding-right: 0px;
  .logoBox {
    align-items: center;
    display: flex;
  }
  .btnLogout {
    height: 40px;
  }
}
.el-aside {
  background-color: #666;
  .el-menu {
    border-right: 0px;
  }
}
.el-main {
  background-color: #fff;
}
.toggle-button {
  background-color: rebeccapurple;
  text-align: center;
  line-height: 30px;
  font-size: 12px;
  cursor: pointer;
  letter-spacing: 0.2em;
}
</style>
